/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiEfetuarLogin.java
 *
 * Created on 23/11/2011, 13:28:24
 */
package view.gui;

import java.awt.Color;
import java.awt.Point;
import model.dominio.Usuario;
import controller.ControllerUsuario;

/**
 *
 * @author danielgr
 * Classe responsável pelo Controle de Usuários do Sistema.
 * Solicita login inicial para disponibilização dos menus de interação do sistema.
 */
public class GuiEfetuarLogin extends JInternalFramePermissao {

    /** Creates new form GuiEfetuarLogin */
    /**
     * Construtor da interface gráfica para tela de login inicial.
     * Além da construção, impede o uso dos menus do sistema, liberando-os apenas após confirmação do cadastro.
     */
    public GuiEfetuarLogin() {
        initComponents();
        loading.setVisible(false);
        Point x = this.getLocation();
        this.setLocation((int) x.getX(), (int) x.getY() - 60);
        this.requestFocus();
    }

    /**
     * Método para controle de login.
     * Cria um novo objeto da classe Usuário e o compara com o banco de dados.
     * Liberação do sistema só acontece se login e senha forem compatíveis com banco de dados.
     */
    public void login() {
        Usuario usuario = new Usuario();
        usuario.setLogin(user.getText());
        usuario.setSenha(String.valueOf(password.getPassword()));
        boolean login = ControllerUsuario.login(usuario);
        if (login) {
            GuiPrincipal.efetuarLogin();
            this.dispose();
        } else {
            loading.setVisible(false);
            lblPwd.setVisible(true);
            password.setVisible(true);
        }
    }

    @Override
    public int getPermissao() {
        return 1;
    }

    /**
     * Método para exibição de barra de carregamento.
     * Exibida após o preenchimento do login e da senha, enquanto busca no banco de dados as informações.
     */
    public class fill implements Runnable {

        public void run() {
            lblPwd.setVisible(false);
            password.setVisible(false);
            loading.setVisible(true);
            loading.repaint();
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            login();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        user = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        lblPwd = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        password = new javax.swing.JPasswordField();
        loading = new javax.swing.JProgressBar();

        setTitle("Login de Usuário");
        getContentPane().setLayout(null);

        user.setForeground(new java.awt.Color(153, 153, 153));
        user.setText("user");
        user.setCaretColor(new java.awt.Color(153, 153, 153));
        user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userActionPerformed(evt);
            }
        });
        user.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                userFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                userFocusLost(evt);
            }
        });
        getContentPane().add(user);
        user.setBounds(110, 40, 208, 30);

        jLabel1.setText("login");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 40, 90, 30);

        lblPwd.setText("password");
        getContentPane().add(lblPwd);
        lblPwd.setBounds(10, 90, 100, 30);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/Login_in-48.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(262, 130, 60, 60);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Locarrão Rent-a-Car : Controle de Usuário");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(0, 0, 340, 30);

        password.setForeground(new java.awt.Color(153, 153, 153));
        password.setText("aaaaaa");
        password.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordActionPerformed(evt);
            }
        });
        password.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                passwordFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                passwordFocusLost(evt);
            }
        });
        getContentPane().add(password);
        password.setBounds(110, 90, 210, 30);

        loading.setForeground(new java.awt.Color(204, 255, 102));
        loading.setFocusable(false);
        loading.setIndeterminate(true);
        getContentPane().add(loading);
        loading.setBounds(10, 90, 310, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-345)/2, (screenSize.height-226)/2, 345, 226);
    }// </editor-fold>//GEN-END:initComponents

   private void userFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_userFocusGained
       if (user.getText().equals("user")) {
           user.setText("");
       }
       user.setForeground(Color.black);
   }//GEN-LAST:event_userFocusGained

   private void passwordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordFocusGained
       if (String.valueOf(password.getPassword()).equals("aaaaaa")) {
           password.setText("");
       }
       password.setForeground(Color.black);
   }//GEN-LAST:event_passwordFocusGained

   private void userFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_userFocusLost
       if (user.getText().equals("")) {
           user.setText("user");
           user.setForeground(new Color(153, 153, 153));
       }
   }//GEN-LAST:event_userFocusLost

   private void passwordFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordFocusLost
       if (String.valueOf(password.getPassword()).equals("")) {
           password.setText("aaaaaa");
           password.setForeground(new Color(153, 153, 153));
       }
   }//GEN-LAST:event_passwordFocusLost

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       new Thread(new fill()).start();
   }//GEN-LAST:event_jButton1ActionPerformed

   private void userActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userActionPerformed
       new Thread(new fill()).start();
   }//GEN-LAST:event_userActionPerformed

   private void passwordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordActionPerformed
       new Thread(new fill()).start();
   }//GEN-LAST:event_passwordActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblPwd;
    private javax.swing.JProgressBar loading;
    private javax.swing.JPasswordField password;
    private javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables
}
